package lin

import (
	"errors"
	"fmt"
	"os"
	"runtime"
)

type G谷歌窗口 struct {
	谷歌程序 C程序
	谷歌路径 string
}

func (类 *G谷歌窗口) Z置谷歌路径(谷歌路径 string) {
	类.谷歌路径 = 谷歌路径
}

func (类 *G谷歌窗口) C创建窗口(网站url string, 宽, 高 int, 是否等待结束 bool) (返回错误 error) {
	dir, err := os.MkdirTemp("", "lorca")
	if err != nil {
		return err
	}
	var 基础谷歌配置 = []string{
		fmt.Sprintf("--app=%s", 网站url),
		fmt.Sprintf("--window-size=%d,%d", 宽, 高),
		fmt.Sprintf("--user-data-dir=%s", dir),
		"--remote-debugging-port=0",
		"--disable-background-networking",
		"--disable-background-timer-throttling",
		"--disable-backgrounding-occluded-windows",
		"--disable-breakpad",
		"--disable-client-side-phishing-detection",
		"--disable-default-apps",
		"--disable-dev-shm-usage",
		"--disable-infobars",
		"--disable-extensions",
		"--disable-features=site-per-process",
		"--disable-hang-monitor",
		"--disable-ipc-flooding-protection",
		"--disable-popup-blocking",
		"--disable-prompt-on-repost",
		"--disable-renderer-backgrounding",
		"--disable-sync",
		"--disable-translate",
		"--disable-windows10-custom-titlebar",
		"--metrics-recording-only",
		"--no-first-run",
		"--no-default-browser-check",
		"--safebrowsing-disable-auto-update",
		//"--enable-automation",
		"--password-store=basic",
		"--use-mock-keychain",
	}

	类.谷歌程序.F附加运行条件(基础谷歌配置...)
	if 类.谷歌路径 == "" {
		类.谷歌路径 = 类.Q取谷歌浏览器地址()
	}
	if 类.谷歌路径 == "" {
		返回错误 = errors.New("错误:谷歌路径不存在")
		return

	}
	返回错误 = 类.谷歌程序.Y运行程序(类.谷歌路径, 是否等待结束)
	return
}

func (类 *G谷歌窗口) G关闭谷歌窗口() error {
	return 类.谷歌程序.G关闭程序()
}

func (类 *G谷歌窗口) D等待谷歌窗口关闭() error {
	return 类.谷歌程序.D等待程序关闭()

}

func (类 *G谷歌窗口) Q运行谷歌程序路径() (谷歌路径 string, 返回错误 error) {
	return 类.谷歌程序.Q取运行程序路径()
}

func (G谷歌窗口) Q取谷歌浏览器地址() string {

	// If env variable "LORCACHROME" specified and it exists
	if path, ok := os.LookupEnv("LORCACHROME"); ok {
		if _, err := os.Stat(path); err == nil {
			return path
		}
	}

	var paths []string
	switch runtime.GOOS {
	case "darwin":
		paths = []string{
			"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
			"/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
			"/Applications/Chromium.app/Contents/MacOS/Chromium",
			"/usr/bin/google-chrome-stable",
			"/usr/bin/google-chrome",
			"/usr/bin/chromium",
			"/usr/bin/chromium-browser",
		}
	case "windows":
		paths = []string{
			"./Chrome/chrome.exe",
			os.Getenv("LocalAppData") + "/Google/Chrome/Application/chrome.exe",
			os.Getenv("ProgramFiles") + "/Google/Chrome/Application/chrome.exe",
			os.Getenv("ProgramFiles(x86)") + "/Google/Chrome/Application/chrome.exe",
			os.Getenv("LocalAppData") + "/Chromium/Application/chrome.exe",
			os.Getenv("ProgramFiles") + "/Chromium/Application/chrome.exe",
			os.Getenv("ProgramFiles(x86)") + "/Chromium/Application/chrome.exe",
			os.Getenv("ProgramFiles(x86)") + "/Microsoft/Edge/Application/msedge.exe",
		}
	default:
		paths = []string{
			"/usr/bin/google-chrome-stable",
			"/usr/bin/google-chrome",
			"/usr/bin/chromium",
			"/usr/bin/chromium-browser",
			"/snap/bin/chromium",
		}
	}

	for _, path := range paths {
		if _, err := os.Stat(path); os.IsNotExist(err) {
			continue
		}
		return path
	}
	return ""
}
